#include "admin.h"
#include "ui_admin.h"
#include<QDebug>
#include<QSqlError>

#include <QFile>
#include <QDir>
#include <QFileDialog>

admin::admin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::admin)
{
    ui->setupUi(this);
    ui->dateTimeEdit_4->setDateTime(QDateTime::currentDateTime());
    //处理更新信息 传递到主页面sqlinfo
    connect(&admin_edit1,admin_edit::send_currentinfo_flush,this,admin::info_flush);
    connect(ui->pushButton_15, &QPushButton::clicked, this, &admin::stopcx);
    connect(ui->pushButton_13, &QPushButton::clicked, this, &admin::stopcx1);
    //车位表管理
    connect(ui->pushButton, &QPushButton::clicked, this, &admin::stopinfoedit);
    connect(ui->pushButton_10, &QPushButton::clicked, this, &admin::stopinfocx);
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &admin::timerUpdate);
    timer->start(1000);

}

admin::~admin()
{
    delete ui;
}

void admin::info_flush(){
    emit(send_info_flush());
}
void admin::printinfo(QString user_id, QString name, QString flag, QString carid, QString phone, QString email, QString register_time,QString fare,QString username){
    ui->lineEdit_5->setText(name);//姓名
    ui->lineEdit_6->setText(phone);//电话
    ui->lineEdit_7->setText(email);//邮箱
    ui->lineEdit_8->setText(flag);//类型
    ui->lineEdit_9->setText(register_time);//注册时间
    ui->lineEdit_10->setText(fare);//余额

    //查询当前用户拥有的汽车
    //QString inquire_2 = QString("select carid as car,cartype as type from carinfo where username = '%1'").arg(username);
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_5);
    this->user=username;
    mode->setTable("carinfo");
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
    //查询操作
    mode->setFilter(QObject::tr("username = '%1'").arg(user)); //查询当前用户
    mode->select();
    mode->removeColumn(0);//不显示第一列id
    mode->removeColumn(3);//不显示第3列username
    mode->setHeaderData(0,Qt::Horizontal,tr("car"));//设置表头车牌号 中文报错未解决
    mode->setHeaderData(1, Qt::Horizontal, tr("type"));//
    mode->submitAll();
    ui->tableView_5->setModel(mode);
}

//个人信息编辑



//刷新再次查询汽车表
void admin::on_pushButton_22_clicked()
{
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_5);
    mode->setTable("carinfo");
    mode->setFilter(QObject::tr("username = '%1'").arg(user)); //查询当前用户
    mode->select();
    mode->removeColumn(0);//不显示第一列id
    mode->removeColumn(3);//不显示第3列username
    mode->setHeaderData(0,Qt::Horizontal,tr("car"));//设置表头车牌号 中文报错未解决
    mode->setHeaderData(1, Qt::Horizontal, tr("type"));//
    ui->tableView_5->setModel(mode);
    QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("刷新成功"), QMessageBox::Yes);
}
//查询所有用户表
void admin::on_pushButton_19_clicked()
{
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_2);
    mode->setTable("userinfo");
    mode->select();
    mode->removeColumn(7);//不显示第七列密码/
    ui->tableView_2->setModel(mode);
    QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
}
//用户类型查询
void admin::on_pushButton_18_clicked()
{
    QString str=ui->comboBox_2->currentText();
    //qDebug()<<str;
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_2);
    mode->setTable("userinfo");
    mode->removeColumn(7);//不显示第七列密码//删除了一列
    if(str=="管理员"||str=="年费用户"||str=="季费用户"||str=="月费用户"||str=="普通用户"){
        mode->setFilter(QObject::tr("flag = '%1'").arg(str)); //查询当前用户
        mode->select();
        ui->tableView_2->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }

   else if(str=="注册时间排序"){
        mode->setSort(7,Qt::DescendingOrder);//降序
        mode->select();
        ui->tableView_2->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else if(str=="余额排序"){

        mode->setSort(10,Qt::DescendingOrder);//降序//fare变为第10
        mode->select();
        //qDebug()<<mode->lastError().text();
        ui->tableView_2->setModel(mode);
        if(mode->lastError().text()==" ")
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
        else
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}
//退出
void admin::on_pushButton_6_clicked()
{
    this->close();
    emit(send_show_login());
}
//打开编辑页面
void admin::on_pushButton_17_clicked()
{
    admin_edit1.show();

}
//汽车类型查询
void admin::on_pushButton_23_clicked()
{
    QString str=ui->comboBox_3->currentText();
    //qDebug()<<str;
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_6);
    mode->setTable("carinfo");
    if(mode->lastError().text()==" ")
    {
        mode->setFilter(QObject::tr("cartype = '%1'").arg(str)); //查询当前类型的汽车
        mode->select();
        ui->tableView_6->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}
//汽车全部查询
void admin::on_pushButton_20_clicked()
{
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_6);
    mode->setTable("carinfo");
    if(mode->lastError().text()==" ")
    {
        mode->select();
        ui->tableView_6->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}
//跳转到汽车编辑页面
void admin::on_pushButton_21_clicked()
{
    caredit2.setuser(user);
    caredit2.setflag(flag);
    caredit2.show();
}
//所属用户查询
void admin::on_pushButton_24_clicked()
{
    QString username=ui->lineEdit_11->text();
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_6);
    mode->setTable("carinfo");
    if(mode->lastError().text()==" ")
    {
        mode->setFilter(QObject::tr("username = '%1'").arg(username)); //查询当前类型的汽车
        mode->select();
        ui->tableView_6->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}

void admin::on_pushButton_15_clicked()
{

}
//停车查询
void admin::stopcx(){

    QDateTime statime = ui->dateTimeEdit_3->dateTime();
    QDateTime endtime = ui->dateTimeEdit_4->dateTime();
        if(statime >= endtime){
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("请输入的结束时间"), QMessageBox::Yes);
        }
        else{
            emit(send_main_stopcx(statime, endtime));
        }
    }
void admin::stopcx1(){
    QString parkingname = ui->lineEdit_4->text();
    if(parkingname.isEmpty()){
    QMessageBox::warning(nullptr, QStringLiteral("车位查询"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }else
    {
        emit(send_main_stopcx1(parkingname));
    }
}
void admin::timerUpdate(){

}
void admin::printstop(QDateTime statime,QDateTime endtime){
    QString statime1;
    QString endtime1;
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView_4);
    mode->setTable("stopinfo");
    statime1=statime.toString("yyyy-MM-dd hh:mm:ss");
    endtime1=endtime.toString("yyyy-MM-dd hh:mm:ss");
    //qDebug()<<"stuuu"<<statime1;
    //qDebug()<<"ethhhh"<<endtime1;
    if(mode->lastError().text()==" ")
    {
        mode->setFilter(QObject::tr("intime >='%1' and outtime<='%2'").arg(statime1).arg(endtime1)); //查询当前类型的汽车
        mode->select();
        mode->setHeaderData(0,Qt::Horizontal,"停车号码");
        mode->setHeaderData(1,Qt::Horizontal,"用户号码");
        mode->setHeaderData(2,Qt::Horizontal,"车牌号码");
        mode->setHeaderData(3,Qt::Horizontal,"停车位号");
        mode->setHeaderData(4,Qt::Horizontal,"进场时间");
        mode->setHeaderData(5,Qt::Horizontal,"出场时间");
        ui->tableView_4->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}
//打印车位查询停车信息
void admin::printstop1(QString parkingname){

     QSqlTableModel*mode= new QSqlTableModel(ui->tableView_4);
      mode->setTable("stopinfo");
      if(mode->lastError().text()==" ")
      {
          mode->setFilter(QObject::tr("place_id='%1'").arg(parkingname)); //查询当前类型的汽车
          mode->select();
          mode->setHeaderData(0,Qt::Horizontal,"停车号码");
          mode->setHeaderData(1,Qt::Horizontal,"用户号码");
          mode->setHeaderData(2,Qt::Horizontal,"车牌号码");
          mode->setHeaderData(3,Qt::Horizontal,"停车位号");
          mode->setHeaderData(4,Qt::Horizontal,"进场时间");
          mode->setHeaderData(5,Qt::Horizontal,"出场时间");
          ui->tableView_4->setModel(mode);
          QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
          ui->lineEdit_4->setText("");
      }
      else{
          QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
      }
}

void admin::stopinfoedit(){

    QString placeid=ui->textEdit->toPlainText();
    QString statusid=ui->comboBox_10->currentText();
    if(placeid.isEmpty()||statusid.isEmpty())
    {
    QMessageBox::warning(nullptr, QStringLiteral("修改"), QStringLiteral("修改失败"), QMessageBox::Yes);
    }else{
    QMessageBox::warning(nullptr, QStringLiteral("修改"), QStringLiteral("修改成功"), QMessageBox::Yes);
    emit(send_main_stopinfoedit(placeid,statusid));}
}

void admin::updatestop(QString placeid,QString statusid){

    int id=placeid.toInt();
    int status=statusid.toInt();
    //qDebug()<<"wwww"<<id;
    this->query.prepare("UPDATE  place  SET status =:x WHERE id =:id ");
    this->query.bindValue(":id",id);
    this->query.bindValue(":x",status);
    this->query.exec();

}
void admin::stopinfocx(){


    QMessageBox::warning(nullptr, QStringLiteral("查询"), QStringLiteral("查询成功"), QMessageBox::Yes);
    emit(send_main_stopinfocx());

}
//查看规则

//修改规则
void admin::on_pushButton_62_clicked()
{
    int id=this->ui->textEdit_7->toPlainText().toInt();
    QString x=this->ui->textEdit_10->toPlainText();
    double y=this->ui->textEdit_9->toPlainText().toDouble();
    this->query.prepare("UPDATE  regular  SET regular_time =:x,per_time_money =:y WHERE id =:id ");

    this->query.bindValue(":id",ui->textEdit_7->toPlainText().toInt());
    this->query.bindValue(":x",this->ui->textEdit_10->toPlainText());
    this->query.bindValue(":y",this->ui->textEdit_9->toPlainText().toDouble());
    this->query.exec();
    if(id!=NULL && x!=NULL && y!=NULL)
        QMessageBox::warning(nullptr, QStringLiteral("修改"), QStringLiteral("修改成功"), QMessageBox::Yes);
    else
        QMessageBox::warning(nullptr, QStringLiteral("修改"), QStringLiteral("修改失败"), QMessageBox::No);
}




//修改车位状态 connect
void admin::on_pushButton_clicked()
{

}
//车位查询 connect
void admin::on_pushButton_10_clicked()
{

}
//查询规则
void admin::on_regular_clicked()
{
    ui->textEdit_8->clear();
    QString str="id  regular_time  per_time_money\n\n";
    ui->textEdit_8->append(str);
    if(this->query.exec("select * from regular")){
        QMessageBox::warning(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询错误"), QMessageBox::Yes);
    }
    qDebug()<<"规则"<<query.lastError().text();
    while(this->query.next())
    {
        QString s=this->query.value(0).toString()+"      "+this->query.value(2).toString()+"      "+this->query.value(3).toString()+"\n\n";
        ui->textEdit_8->append(s);
    }
}
//预订全部查询
void admin::on_pushButton_16_clicked()
{
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView);
    mode->setTable("book");
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
    //查询操作
    mode->select();

    mode->setHeaderData(0,Qt::Horizontal,"预订号码");
    mode->setHeaderData(1,Qt::Horizontal,"用户号码");
    mode->setHeaderData(2,Qt::Horizontal,"车牌号码");
    mode->setHeaderData(3,Qt::Horizontal,"停车位号");
    mode->setHeaderData(4,Qt::Horizontal,"预订时间");
    mode->setHeaderData(5,Qt::Horizontal,"预订状态");
    mode->submitAll();
    ui->tableView->setModel(mode);
}
//预订时间查询
void admin::on_pushButton_7_clicked()
{
    QDateTime statime=ui->dateTimeEdit_6->dateTime();
    QDateTime endtime=ui->dateTimeEdit_5->dateTime();
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView);
    mode->setTable("book");
    QString statime1=statime.toString("yyyy-MM-dd hh:mm:ss");
    QString endtime1=endtime.toString("yyyy-MM-dd hh:mm:ss");

    if(mode->lastError().text()==" ")
    {
        mode->setFilter(QObject::tr("time >='%1' and time<='%2'").arg(statime1).arg(endtime1)); //查询当前类型的汽车
        mode->select();
        mode->setHeaderData(0,Qt::Horizontal,"预订号码");
        mode->setHeaderData(1,Qt::Horizontal,"用户号码");
        mode->setHeaderData(2,Qt::Horizontal,"车牌号码");
        mode->setHeaderData(3,Qt::Horizontal,"停车位号");
        mode->setHeaderData(4,Qt::Horizontal,"预订时间");
        mode->setHeaderData(5,Qt::Horizontal,"预订状态");
        ui->tableView->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}
//车位查询
void admin::on_pushButton_8_clicked()
{
    QString place_id=ui->lineEdit_2->text();
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView);
    mode->setTable("book");

    if(mode->lastError().text()==" ")
    {
        mode->setFilter(QObject::tr("place_id='%1'").arg(place_id)); //查询当前类型的汽车
        mode->select();
        mode->setHeaderData(0,Qt::Horizontal,"预订号码");
        mode->setHeaderData(1,Qt::Horizontal,"用户号码");
        mode->setHeaderData(2,Qt::Horizontal,"车牌号码");
        mode->setHeaderData(3,Qt::Horizontal,"停车位号");
        mode->setHeaderData(4,Qt::Horizontal,"预订时间");
        mode->setHeaderData(5,Qt::Horizontal,"预订状态");
        ui->tableView->setModel(mode);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
    }
}
//状态查询
void admin::on_pushButton_9_clicked()
{

        QString status=ui->comboBox_4->currentText();
        QSqlTableModel*mode= new QSqlTableModel(ui->tableView);
        mode->setTable("book");

        if(mode->lastError().text()==" ")
        {
            mode->setFilter(QObject::tr("status='%1'").arg(status)); //查询当前类型的汽车
            mode->select();
            mode->setHeaderData(0,Qt::Horizontal,"预订号码");
            mode->setHeaderData(1,Qt::Horizontal,"用户号码");
            mode->setHeaderData(2,Qt::Horizontal,"车牌号码");
            mode->setHeaderData(3,Qt::Horizontal,"停车位号");
            mode->setHeaderData(4,Qt::Horizontal,"预订时间");
            mode->setHeaderData(5,Qt::Horizontal,"预订状态");
            ui->tableView->setModel(mode);
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("查询成功"), QMessageBox::Yes);
        }
        else{
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("查询失败"), QMessageBox::Yes);
        }
}

//生成预约流量报表
void admin::on_pushButton_2_clicked()
{
    form.book_his_show();
    form.book_bar_show();
    form.stop_his_show();
    form.stop_bar_show();
    form.con_his_show();
    form.con_bar_show();
    form.show();
}

//导出结算报表csv文件
void admin::on_pushButton_5_clicked()
{
    QString cmd;
        QSqlQueryModel *model;
        model = new QSqlQueryModel();
        //查询结算表中所有内容
        cmd += "select * from consuption";
        cmd += ui->lineEdit->text();
        model->setQuery(cmd);
        ui->tableView->setModel(model);

        //以下是保存csv
        int row,col,i,j;
        QFile file;
        QString fileName;
        QModelIndex index;
        row = model->rowCount();            //获取当前tableview行列数
        col = model->columnCount();
        fileName = QFileDialog::getSaveFileName(this,
                                                tr("保存表格"),
                                                QDir::currentPath() + "/" +\
                                                ui->lineEdit->text() + ".csv",
                                                tr("csv File(*.csv)"));
        if(fileName.isNull())
        {
            return;
        }

        file.setFileName(fileName);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            qDebug()<<"open file fail";
            return;
        }
        QTextStream out(&file);             //创建一个文本流，向保存文件中写入文本

        //写表头
        for(i = 0;i < col; i++)
        {
            out<<model->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString()<<",";
            if(i == (col - 1))
            {
                out<<"\n";
            }
        }
        //写数据
        for(i = 0;i < row; i++)
        {
            for(j = 0;j < col; j++)
            {
                index = model->index(i,j);
                out<<model->data(index).toString()<<",";
                if(j == (col - 1))
                {
                    out<<"\n";
                }
            }
        }
        file.close();
}
